package com.bawei.bydayzg_two.viewModel

import androidx.lifecycle.viewModelScope
import com.bawei.base.bean.CreateOrderResult
import com.bawei.base.bean.GetPreOrderResult
import com.bawei.base.bean.PayResult
import com.bawei.base.bean.Res
import com.bawei.base.http.SingleLiveData
import com.bawei.base.mvvm.BaseViewModel
import com.bawei.base.param.PCreatOrder
import com.bawei.base.param.PPay
import kotlinx.coroutines.launch

class GetPreViewModel:BaseViewModel() {

    val getPreSuccess = SingleLiveData<Res<GetPreOrderResult>>()
    val getPreFail = SingleLiveData<String>()
    //
    fun getPreOrder(preOrderNo:String){
        viewModelScope.launch {
            try {
                getPreSuccess.postValue(apiService.getPreOrder(preOrderNo))
            } catch (e: Exception) {
                getPreFail.postValue(e.message.toString())
            }
        }
    }

    //
    val createOrderSuccess = SingleLiveData<Res<CreateOrderResult>>()
    val createOrderFail = SingleLiveData<String>()
    //
    fun createOrder(preOrderNo: PCreatOrder){
        viewModelScope.launch {
            try {
                createOrderSuccess.postValue(apiService.createOrder(preOrderNo))
            } catch (e: Exception) {
                createOrderFail.postValue(e.message.toString())
            }
        }
    }

    //
    val paySuccess = SingleLiveData<Res<PayResult>>()
    val payFail = SingleLiveData<String>()
    //
    fun pay(pPay: PPay){
        viewModelScope.launch {
            try {
                paySuccess.postValue(apiService.pay(pPay))
            } catch (e: Exception) {
                payFail.postValue(e.message.toString())
            }
        }
    }
}